<div class="layui-fluid">
    <div class="layui-card">
        <!-- 搜索表单区 -->
        <form class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="sys_dic-search-form">
            <div class="layui-form-item">
    
        
        
            
            
              
                <!-- 默认表单 -->
                <div class="layui-inline">
                    <label class="layui-form-label">字典编号</label>
                    <div class="layui-input-inline">
                        <input type="text" name="dic_id" placeholder="请输入字典编号" autocomplete="off" class="layui-input">
                    </div>
                </div>
              
            
        
    
        
        
            
            
              
                <!-- 默认表单 -->
                <div class="layui-inline">
                    <label class="layui-form-label">字典代码</label>
                    <div class="layui-input-inline">
                        <input type="text" name="dic_code" placeholder="请输入字典代码" autocomplete="off" class="layui-input">
                    </div>
                </div>
              
            
        
    
        
        
            
            
              
                <!-- 默认表单 -->
                <div class="layui-inline">
                    <label class="layui-form-label">字典名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="dic_name" placeholder="请输入字典名称" autocomplete="off" class="layui-input">
                    </div>
                </div>
              
            
        
    
        
        
            
            
              
                <!-- 默认表单 -->
                <div class="layui-inline">
                    <label class="layui-form-label">字典值</label>
                    <div class="layui-input-inline">
                        <input type="text" name="dic_value" placeholder="请输入字典值" autocomplete="off" class="layui-input">
                    </div>
                </div>
              
            
        
    
        
        
            
            
              
                <!-- 时间表单 -->
                <div class="layui-inline">
                    <label class="layui-form-label">创建时间</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" id="create_datetime_search_datetime" placeholder="yyyy-MM-dd HH:mm:ss" >
                    </div>
                </div>
                  
            
        
    
        
        
            
            
              
                <!-- 布尔表单 -->
                <div class="layui-inline" >
                    <label class="layui-form-label">是否启用</label>
                    <div class="layui-input-block">
                        <select name="is_enable" lay-verify="">
                            <option value="">请选择</option>
                            <option value="1">是</option>
                            <option value="0">否</option>
                        </select>
                    </div>
                </div>
                  
            
        
    
        
        
            
            
                <!-- 下拉表单 -->
                <div class="layui-inline">
                    <label class="layui-form-label">上级编号</label>
                    <div class="layui-input-inline">
                        <select id="dic_pid_search_select"  name="dic_pid" xm-select-search="" xm-select-radio=""
                                placeholder="请选择上级编号" autocomplete="off" xm-select-height="36px" xm-select-skin="normal" xm-select="dic_pid_search_select" lay-filter="dic_pid_search_select" >
                            <option value="">请选择上级编号</option>
                        </select>
                    </div>
                </div>
              
        
    
                <div class="layui-inline">
                    <button class="layui-btn btn-sys_dic-search" lay-submit lay-filter="btn-sys_dic-form-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>

        <!-- 列表区 -->
        <div class="layui-card-body">

            <!-- 头部工具栏模板 -->
            <script type="text/html" id="sys_dic_header_toolbar">
                <div class="layui-btn-container">
                    <button class="layui-btn" lay-event="add">添加</button>
                    <button class="layui-btn layui-btn-danger" lay-event="dels">删除</button>
                </div>
            </script>

            <!-- 列表表格 -->
            <table id="sys_dic-table" lay-filter="sys_dic-table" style="width: 100%"></table>
     
      
        
      
     
      
        
      
     
      
        
      
     
      
        
      
     
      
        
      
     
      
        
            <!-- 是否启用列表显示模板 -->
            <script type="text/html" id="is_enable_templet">
                <b style="color:{{ d.is_enable?'green':'red' }};">{{ d.is_enable ?'是':'否' }}</b>
            </script>
        
      
     
      
        
      
    


            <!-- 每行操作按钮模板 -->
            <script type="text/html" id="sys_dic_row_toolbar">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>
<!-- JS脚本区 -->
<script>
    layui.use(['table', 'form', 'layer','laydate', 'jquery', 'admin', 'formSelects'], function () {
        var form = layui.form, layer = layui.layer, laydate = layui.laydate, table = layui.table, admin = layui.admin, formSelects = layui.formSelects;

        form.render(null, 'sys_dic-search-form');

 
  
    
    
        
    
  
 
  
    
    
        
    
  
 
  
    
    
        
    
  
 
  
    
    
        
    
  
 
  
    
    
        
          //渲染创建时间日期时间选择器
          laydate.render({ elem: '#create_datetime_search_datetime' ,type: 'datetime' });
        
    
  
 
  
    
    
        
    
  
 
  
    
    
        //渲染上级编号下拉分页选择器
        formSelects.data('dic_pid_search_select', 'server', {
            url: '/system/dic/list',
            keyName: 'dic_name',
            keyVal: 'dic_id',
            searchName: 'dic_name__op__like',
            showPage: true,
            size: 10
        });
      
  

        // 表单搜索监听
        form.on('submit(btn-sys_dic-form-search)', function (data) {
            console.log('sys_dic-search-form', data)
            var field = data.field;
            //执行重载
            table.reload('sys_dic-table', {
                where: field,
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false;
        });

        // 渲染表格数据
        table.render({
            id: 'sys_dic-table',
            elem: '#sys_dic-table',
            toolbar: '#sys_dic_header_toolbar',
            url: '/system/dic/list',
            loading: true,
            page: true, //开启分页
            cols: [[
                {type: 'checkbox', fixed: 'left'},
         
            
            
                
                    {field: 'dic_id', title: '字典编号'/*, event: 'dic_id', templet: "#dic_id_templet"*/},
                
            
          
         
            
            
                
                    {field: 'dic_code', title: '字典代码'/*, event: 'dic_code', templet: "#dic_code_templet"*/},
                
            
          
         
            
            
                
                    {field: 'dic_name', title: '字典名称'/*, event: 'dic_name', templet: "#dic_name_templet"*/},
                
            
          
         
            
            
                
                    {field: 'dic_value', title: '字典值'/*, event: 'dic_value', templet: "#dic_value_templet"*/},
                
            
          
         
            
            
                
                    {field: 'create_datetime', title: '创建时间'/*, event: 'create_datetime', templet: "#create_datetime_templet"*/},
                
            
          
         
            
            
                
                    {field: 'is_enable', title: '是否启用', event: 'is_enable', templet: "#is_enable_templet"},
                  
            
          
         
            
            
                {field: 'dic_pid', title: '上级编号', templet: function (row){
                    if(row.dic_pid_title){
                        return row.dic_pid_title;
                    }
                    return '--'/*row.dic_pid*/;
                }},
            
          
        
                {title: '操作', fixed: 'right', templet: '#sys_dic_row_toolbar'}
            ]],
            done: function () {}
        });

        // 监听表格工具栏事件
        table.on("toolbar(sys_dic-table)", eventListener)

        // 监听表格行事件
        table.on("tool(sys_dic-table)", eventListener)

        
                      
        ;
        // 事件监听
        function eventListener(obj) {
            let event = obj.event;
            let data = obj.data;
            switch (event) {
                case 'add':
                    layui.$.post('/system/dic/editPage', function (str) {
                        layer.open({
                            type: 1,
                            title: '新建数据字典',
                            maxmin: true,
                            area: ['600px', '482px'],
                            content: str,
                        })
                    });
                    break;
                case 'edit':
                    layui.$.post('/system/dic/editPage', {dic_id: data.dic_id}, function (str) {
                        layer.open({
                            type: 1,
                            title: '编辑数据字典',
                            maxmin: true,
                            area: ['600px', '482px'],
                            content: str,
                        })
                    });
                    break;
                case 'dels':
                    let checkStatus = table.checkStatus('sys_dic-table');
                    let selectRow = checkStatus.data;
                    deleteRow(selectRow);
                    break;
                case 'del':
                    let row = obj.data;
                    deleteRow([row]);
                    break;
            }
            console.log('toolbar_event', obj)
        }

        // 删除行方法
        function deleteRow(selectRows) {
            if (selectRows.length <= 0) {
                layer.msg('请先钩选要删除的记录行！');
                return;
            }
            layer.confirm('确定要删除这' + selectRows.length + '条记录吗？',
                {icon: 3, title: '提示', btn: ['确认删除', '取消']},
                function (index) {
                    console.log(index);
                    layui.$.ajax({
                        url: '/system/dic/delete',
                        data: {
                            ids: selectRows.map((row) => row.dic_id)
                        },
                        traditional: true,
                        type: 'POST',
                        success: function (result) {
                            if (result.success) {
                                layer.msg('删除成功', {icon: 6});
                                table.reload('sys_dic-table');
                            } else {
                                layer.msg('删除失败' + result.error.message, {icon: 5});
                            }
                        }
                    });
                });

        }

    })
</script>